package com.xiaoshuidi.cloud.module.contract.controller.admin.contract;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.contractTemplate.*;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.TemplateKeywardsDO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "合同管理 - 合同模板")
@RestController
@RequestMapping(ApiConstants.NAME +"/template")
@Validated
public class ContractTemplateController {

    @Resource
    private ContractTemplateService contractTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建合同模板")
    @PreAuthorize("@ss.hasPermission('contract:template:create')")
    public CommonResult<Long> createContractTemplate(@Valid @RequestBody ContractTemplateCreateReqVO createReqVO) {
        return success(contractTemplateService.createContractTemplate(createReqVO));
    }

    @PutMapping("/openOrClose")
    @Operation(summary = "启用&关闭")
    public CommonResult<Boolean> openOrCloseTemplate(@Valid @RequestBody ContractTemplateOpenOrCloseReqVO reqVO) {
        contractTemplateService.openOrCloseContractTemplate(reqVO);
        return success(true);
    }

    @PutMapping("/edit")
    @Operation(summary = "编辑合同模板")
    @PreAuthorize("@ss.hasPermission('contract:template:update')")
    public CommonResult<Boolean> editTemplate(@Valid @RequestBody ContractTemplateUpdateReqVO reqVO) {
        contractTemplateService.updateContractTemplate(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:template:delete')")
    public CommonResult<Boolean> deleteContractTemplate(@RequestParam("id") Long id) {
        contractTemplateService.deleteContractTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ContractTemplateRespVO> getContractTemplate(@RequestParam("id") Long id) {
        ContractTemplateRespVO contractTemplateRespVO = contractTemplateService.getContractTemplate(id);
        return success(contractTemplateRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同模板分页")
    @PreAuthorize("@ss.hasPermission('contract:template:query')")
    public CommonResult<PageResult<ContractTemplateRespVO>> getContractTemplatePage(@Valid ContractTemplatePageReqVO pageVO) {
        PageResult<ContractTemplateRespVO> pageResult = contractTemplateService.getContractTemplatePage(pageVO);
        return success(pageResult);
    }


    @PostMapping("/relationHouse")
    @Operation(summary = "模板关联房源")
    @PreAuthorize("@ss.hasPermission('contract:template:relRoom')")
    public CommonResult<Boolean> relationHouse(@Valid @RequestBody ContractTemplateRelHouseReqVO reqVO) {
        contractTemplateService.relationHouse(reqVO);
        return success(true);
    }

    @GetMapping("/checkName")
    @Operation(summary = "校验模板名称")
    @Parameters({
            @Parameter(name = "name", description = "模板名称", required = true, example = "合房租赁"),
            @Parameter(name = "id", description = "模板ID", example = "1")
    })
    public CommonResult<Boolean> checkName(@RequestParam(required = false) Long id,@RequestParam String name, @RequestParam(required = false) String templateType) {
        return success(contractTemplateService.existName(name, id, templateType));
    }

    @GetMapping("/selected-apartments")
    @Operation(summary = "查询已关联过合同模板的门店")
    @Parameter(name = "id", description = "合同编号", required = true, example = "1024")
    public CommonResult<List<Long>> getSelectedApartments(@RequestParam("id") Long id,@RequestParam String templateType) {
        return success(contractTemplateService.getSelectedApartments(id,templateType));
    }

    /**
     * 分页查询模板关键字
     */
    @GetMapping("/page-keywards")
    public CommonResult<PageResult<TemplateKeywardsDO>> getContractTemplateKeywardsPage(@Valid ContractTemplateKeywordsPageReqVO pageVO) {
        PageResult<TemplateKeywardsDO> pageResult = contractTemplateService.getContractTemplateKeywordsPage(pageVO);
        return success(pageResult);
    }

}
